#pragma once 
#include "TcpServer.hpp"
#include "Protocol.hpp"
#include "LOG.hpp"
#include "Task.hpp"
#include "ThreadPool.hpp"

#define PORT 8081
class HttpServer{
    private:
        int sock;
        int port;
        int stop;
        TcpServer* tcpserver;
    public:
        HttpServer(int _port = PORT):port(_port),stop(false),tcpserver(nullptr){}
        void InitHttpServer(){
            // 防止对端(client and server)断开发送SIGPIPE信号时,执行默认动作
            signal(SIGPIPE,SIG_IGN);
        }

        void Loop(){
            tcpserver = TcpServer::GetInstance(port);
            while(!stop){
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer);
                memset(&peer,0,sizeof(peer));
                sock = accept(tcpserver->GetListenSock(),(struct sockaddr*)&peer,&len);
                LOG(INFO,"Begin Get A New Link!");
                if(sock < 0){
                    LOG(INFO,"Accept Socket Unsucess!");
                    continue;
                }

                LOG(INFO,"Begin Proess Request!");
                TASK task(sock);
                ThreadPool::GetInstance()->PushTask(task);
            }
        }

       ~HttpServer(){}

};
